<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/18
 * Time: 17:35
 */

namespace app\admin\model;
use think\Model;

class MsgModel extends Model{
    // 确定链接表名
    protected $table = 'snake_push';
    function getAll($map){
        return $this->where($map)->count();
    }

    function getMsg($map,$offset,$limit){
        $result=$this->field('*')
            ->where($map)->limit("$offset , $limit")->order('id DESC')->select();
        return $result;
    }
    //添加消息
    public function add($param){
        $param['addtime'] = time();
        $param['from_user']=0;
        if(!paramValid($param,'title')) $param['title']='系统消息';
        try{
            $result = $this->save($param);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url('push/index'), '消息发布成功');
            }
        }catch (\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }
    //删除消息
    public function del($id)
    {
        try{
            $this->where('id', $id)->delete();
            return msg(1, url('push/index'), '删除成功');
        }catch(\Exception $e){
            return msg(-1, '', $e->getMessage());
        }
    }
}